﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Safekeeping
    {
        public static Safekeeping FindBySafekeepingID_IncludeDeleted(linqDatabaseDataContext db, int SafekeepingID)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.SafekeepingID == SafekeepingID select u).SingleOrDefault());
        }

        public static Safekeeping FindBySafekeepingID(linqDatabaseDataContext db, int SafekeepingID)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.SafekeepingID == SafekeepingID select u).SingleOrDefault());
        }

        public static List<Safekeeping> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<Safekeeping> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<Safekeeping> ToListByRequestID_IncludeDeleted(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sSafekeepings where v.RequestID == RequestID select v.ID).Contains(u.SafekeepingID) select u).ToList());
        }

        public static List<Safekeeping> ToListByRequestID(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Safekeepings where u.ID == (from v in db.Safekeepings where v.SafekeepingID == u.SafekeepingID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sSafekeepings where v.RequestID == RequestID select v.ID).Contains(u.SafekeepingID) && !u.IsDeleted select u).ToList());
        }

        public static List<Safekeeping> HistoryToListBySafekeepingID(linqDatabaseDataContext db, int SafekeepingID)
        {
            return (from u in db.Safekeepings where u.SafekeepingID == SafekeepingID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
